package com.artPreter.controller.mobile;

import com.artPreter.pojo.*;
import com.artPreter.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static com.artPreter.utils.PageUtils.getPageList;

@Controller
@RequestMapping("/m/depart")
public class MDepartContorller {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArtistService artistService;
    @Autowired
    private IArtworkService artwrokService;
    @Autowired
    private IDepartService departService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private IDepartTempService departTempService;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private IArtworkOtherPicService artworkOtherPicService;
    @Autowired
    private IArtworkService artWorkService;
    @Autowired
    private IEventExhibitionService eventExhibitionService;
    @Autowired
    private IDepartSectionTempService departSectionTempService;

    @GetMapping("")
    public String depart(Integer id, Model model) {


        String deaprtId = "";

        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        Depart depart = departService.getDepartByInfo(id);
        if(depart != null){
            deaprtId = depart.getDepartId();
        }else{
            return "redirect:/";
        }


        DepartTemp departTemp = new DepartTemp();
        departTemp.setDepartId(deaprtId);
        DepartTemp departTempItem = departTempService.getDepartTemp(departTemp);
        if (departTempItem == null) {
            return "redirect:/";
        }
        DepartSectionTemp departSectionTemp = new DepartSectionTemp();
        departSectionTemp.setDepartId(deaprtId);
        List<DepartSectionTemp> departSectionTemps = departSectionTempService.getList(departSectionTemp);
        if(departSectionTemps.size() >0 ){
            for (int i = 0; i < departSectionTemps.size(); i++) {
                if(departSectionTemps.get(i).getIsDefault().equals(1)){
                    return "redirect:/m/depart/page?id="+id+"&sectionId="+departSectionTemps.get(i).getId();

                }
            }
        }else{
            return "redirect:/";
        }
        // departTemp/getDepartTemp



        return "redirect:/";
    }

    @GetMapping("/artwork/detail")
    public String departArtworkDetail(Integer id,Integer sectionId,Integer wid, Model model) {


        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        Artwork artwork = artWorkService.getArtWorkById(wid);
        if(artwork != null){
            List<ArtType> artTypes = artTypeService.getList();
            for (int i = 0; i < artTypes.size(); i++) {
                if(artTypes.get(i).getTypeId().equals(artwork.getArtwoktType())){
                    artwork.setArtTypeName(artTypes.get(i).getName());
                }
            }
            model.addAttribute("artwork",artwork);
        }



        Depart depart = departService.getDepartByInfo(id);
        String deaprtId = "";
        if(depart == null){
            System.out.println("没有该机构");
            return "redirect:/";
        }else{
            deaprtId = depart.getDepartId();
            model.addAttribute("depart",depart);
        }

        List<ArtworkOtherPic> artworkOtherPics = artworkOtherPicService.getArtworkAllPicList(artwork);
        model.addAttribute("artworkOtherPics",artworkOtherPics);



        DepartSectionTemp departSectionTemp = new DepartSectionTemp();
        departSectionTemp.setDepartId(deaprtId);
        model.addAttribute("departId",id);
        model.addAttribute("sectionId",sectionId);
        List<DepartSectionTemp> departSectionTemps = departSectionTempService.getList(departSectionTemp);
        System.out.println("模板"+departSectionTemps.size()+"条");
        String tempPath = "";

        for (int i = 0; i < departSectionTemps.size(); i++) {
            if(departSectionTemps.get(i).getId().equals(sectionId)){
                model.addAttribute("sectionName",departSectionTemps.get(i).getName());
                tempPath = departSectionTemps.get(i).getTempAddr();
                DepartContentSection departContentSection = new DepartContentSection();
                departContentSection.setSectionId(departSectionTemps.get(i).getSectionId());
                departContentSection.setDepartId(deaprtId);
                List<DepartContentSection> departContentSectionServiceList = departContentSectionService.getList(departContentSection);



                // 单页 1
                List<String> pages = new ArrayList<>();
                // 艺术品 3
                List<Artwork> artworks = new ArrayList<>();
                // 图片

                // 艺术家 2
                List<Artist> artists = new ArrayList<>();
                System.out.println(departContentSectionServiceList.size()+"---------");
                for (int j = 0; j < departContentSectionServiceList.size(); j++) {
                    System.out.println(departContentSectionServiceList.get(j).getContentType()+"aaaaaa");
                    // 单页 1
                    if(departContentSectionServiceList.get(j).getContentType().equals(1)){
                        System.out.println(departContentSectionServiceList.get(j).getContent()+"单页内容");
                        pages.add(departContentSectionServiceList.get(j).getContent());
                    }
                    // 艺术家 2
                    if(departContentSectionServiceList.get(j).getContentType().equals(2)){
                        artists = artistService.getRelatedArtistList(departContentSectionServiceList.get(j));
                        System.out.println("艺术家"+artists.size()+"条");
                        model.addAttribute("artists",artists);
                    }
                    // 艺术品 3
                    if(departContentSectionServiceList.get(j).getContentType().equals(3)){
                        // 小于10代表单列表模板
                        Integer count = 0;
                        if(departSectionTemps.get(i).getContentType()<10){
                            count = -99;
                        }else{
                            count = 6;
                        }
                        // 作品在线
                        if(tempPath.equals("/zpzx")){

                            // 数据
                            tempPath = "pc/depart/depart_artwork_online";
                        }else{
                            artworks = artwrokService.getRelatedArtworkList(departContentSectionServiceList.get(j),count);
                            System.out.println("艺术品"+artworks.size()+"条");
                            model.addAttribute("artworks",artworks);
                        }
                    }
                    // 文章 5
                    if(departContentSectionServiceList.get(j).getContentType().equals(5)){
                    }
                }
                DepartTemp departTempItem = new DepartTemp();
                System.out.println(deaprtId);
                System.out.println("机构3");
                departTempItem.setDepartId(deaprtId);
                DepartTemp departTemp = departTempService.getDepartTemp(departTempItem);
                if(departTemp!= null){
                    System.out.println(departTemp.getBannerUrl());
                    model.addAttribute("departTemp",departTemp);
                }else{
                    return "redirect:/";
                }



                // 模板单页数据
                model.addAttribute("pageDetails",pages);


            }
        }
        //tempPath = "pc/depart/simple_artwork_detail";
        tempPath = "mobile/depart/artwork_detail";
        model.addAttribute("departSectionTemps",departSectionTemps);

        // departTemp/getDepartTemp


        if(tempPath.equals("")){
            return "redirect:/";
        }else{
            return tempPath;
        }
    }

    @GetMapping("/article/detail")
    public String departArticleDetail(Integer id,Integer sectionId,Integer aid, Model model) {


        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        Article article = articleService.getArticleByNumberId(aid);
        if(article != null){

            model.addAttribute("article",article);
        }

        if(article.getLink()!=null&&!article.getLink().equals("")){
            // 文章链接
            String link = article.getLink();
            return "redirect:"+link;
        }



        Depart depart = departService.getDepartByInfo(id);
        String deaprtId = "";
        if(depart == null){
            System.out.println("没有该机构");
            return "redirect:/";
        }else{
            deaprtId = depart.getDepartId();
            model.addAttribute("depart",depart);
        }
        DepartSectionTemp departSectionTemp = new DepartSectionTemp();
        departSectionTemp.setDepartId(deaprtId);
        model.addAttribute("departId",id);
        model.addAttribute("sectionId",sectionId);
        List<DepartSectionTemp> departSectionTemps = departSectionTempService.getList(departSectionTemp);
        System.out.println("模板"+departSectionTemps.size()+"条");
        String tempPath = "";

        for (int i = 0; i < departSectionTemps.size(); i++) {
            if(departSectionTemps.get(i).getId().equals(sectionId)){
                model.addAttribute("sectionName",departSectionTemps.get(i).getName());
                tempPath = departSectionTemps.get(i).getTempAddr();
                DepartContentSection departContentSection = new DepartContentSection();
                departContentSection.setSectionId(departSectionTemps.get(i).getSectionId());
                departContentSection.setDepartId(deaprtId);
                List<DepartContentSection> departContentSectionServiceList = departContentSectionService.getList(departContentSection);



                // 单页 1
                List<String> pages = new ArrayList<>();
                // 艺术品 3
                List<Artwork> artworks = new ArrayList<>();
                // 图片

                // 艺术家 2
                List<Artist> artists = new ArrayList<>();
                System.out.println(departContentSectionServiceList.size()+"---------");
                for (int j = 0; j < departContentSectionServiceList.size(); j++) {
                    System.out.println(departContentSectionServiceList.get(j).getContentType()+"aaaaaa");
                    // 单页 1
                    if(departContentSectionServiceList.get(j).getContentType().equals(1)){
                        System.out.println(departContentSectionServiceList.get(j).getContent()+"单页内容");
                        pages.add(departContentSectionServiceList.get(j).getContent());
                    }
                    // 艺术家 2
                    if(departContentSectionServiceList.get(j).getContentType().equals(2)){
                        artists = artistService.getRelatedArtistList(departContentSectionServiceList.get(j));
                        System.out.println("艺术家"+artists.size()+"条");
                        model.addAttribute("artists",artists);
                    }
                    // 艺术品 3
                    if(departContentSectionServiceList.get(j).getContentType().equals(3)){
                        // 小于10代表单列表模板
                        Integer count = 0;
                        if(departSectionTemps.get(i).getContentType()<10){
                            count = -99;
                        }else{
                            count = 6;
                        }
                        // 作品在线
                        if(tempPath.equals("/zpzx")){

                            // 数据
                            tempPath = "pc/depart/depart_artwork_online";
                        }else{
                            artworks = artwrokService.getRelatedArtworkList(departContentSectionServiceList.get(j),count);
                            System.out.println("艺术品"+artworks.size()+"条");
                            model.addAttribute("artworks",artworks);
                        }
                    }
                    // 文章 5
                    if(departContentSectionServiceList.get(j).getContentType().equals(5)){
                    }
                }
                DepartTemp departTempItem = new DepartTemp();
                System.out.println(deaprtId);
                System.out.println("机构3");
                departTempItem.setDepartId(deaprtId);
                DepartTemp departTemp = departTempService.getDepartTemp(departTempItem);
                if(departTemp!= null){
                    System.out.println(departTemp.getBannerUrl());
                    model.addAttribute("departTemp",departTemp);
                }else{
                    return "redirect:/";
                }



                // 模板单页数据
                model.addAttribute("pageDetails",pages);


            }
        }
        tempPath = "mobile/depart/article_detail";
        model.addAttribute("departSectionTemps",departSectionTemps);

        // departTemp/getDepartTemp


        if(tempPath.equals("")){
            return "redirect:/";
        }else{
            return tempPath;
        }
    }

    @GetMapping("/page")
    public String departPage(Integer id,Integer sectionId,Integer currentPage,Integer type,String name, Model model) {



        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        if(currentPage==null){
            currentPage = 1;
        }
        model.addAttribute("currentPage",currentPage);

        Depart depart = departService.getDepartByInfo(id);
        String deaprtId = "";
        if(depart == null){
            System.out.println("depart == null");
            return "redirect:/";
        }else{
            deaprtId = depart.getDepartId();
            model.addAttribute("depart",depart);
        }
        System.out.println("跳转1");
        DepartSectionTemp departSectionTemp = new DepartSectionTemp();
        departSectionTemp.setDepartId(deaprtId);
        model.addAttribute("type",type);
        model.addAttribute("departId",id);
        model.addAttribute("sectionId",sectionId);
        List<DepartSectionTemp> departSectionTemps = departSectionTempService.getList(departSectionTemp);
        String tempPath = "";
        List<ArtType> artTypes = new ArrayList<>();

        for (int i = 0; i < departSectionTemps.size(); i++) {
            if(departSectionTemps.get(i).getId().equals(sectionId)){
                tempPath = departSectionTemps.get(i).getTempAddr();
                DepartContentSection departContentSection = new DepartContentSection();
                departContentSection.setSectionId(departSectionTemps.get(i).getSectionId());
                departContentSection.setDepartId(deaprtId);
                List<DepartContentSection> departContentSectionServiceList = departContentSectionService.getList(departContentSection);
                // 文章 5
                List<Article> articles = new ArrayList<>();
                // 单页 1
                List<String> pages = new ArrayList<>();
                // 艺术品 3
                List<Artwork> artworks = new ArrayList<>();
                // 图片

                // 艺术家 2
                List<Artist> artists = new ArrayList<>();
                for (int j = 0; j < departContentSectionServiceList.size(); j++) {

                    // 单页 1
                    if(departContentSectionServiceList.get(j).getContentType().equals(1)){

                        pages.add(departContentSectionServiceList.get(j).getContent());
                    }
                    // 艺术家 2
                    if(departContentSectionServiceList.get(j).getContentType().equals(2)){
                        Integer size = 64;
                        // 显示艺术类型
                        artTypes = artTypeService.getList();

                        // 艺术类型搜索
                        if(type!=null){
                            for (int k = 0; k < artTypes.size(); k++) {
                                if (Objects.equals(artTypes.get(k).getId(), type)){
                                    departContentSectionServiceList.get(j).setSearchType(artTypes.get(k).getTypeId());
                                }
                            }
                        }
                        // 艺术家名字模糊搜索
                        if(name!=null&&!name.equals("")){
                            departContentSectionServiceList.get(j).setSearchName(name);
                        }
                        List<ArtType> artTypeList = new ArrayList<>();
                        artTypeList = artistService.getDepartRelatedArtistTypeList(departContentSectionServiceList.get(j));
                        System.out.println(artTypeList);
                        for (int y = 0; y < artTypes.size(); y++) {
                            for (int z = 0; z < artTypeList.size(); z++) {
                                if(artTypeList.get(z).getTypeId().contains(artTypes.get(y).getTypeId())){
                                    artTypes.get(y).setIsShow(1);
                                }
                            }
                        }
                        System.out.println(artTypes);
                        //List<ArtType> artworkTypeList = artwrokService.getArtistRelatedArtworkArtTypegetPageList(departContentSectionServiceList.get(j));
                        IPage<Artist> artistIPage = artistService.getRelatedArtistPageList(departContentSectionServiceList.get(j),currentPage,size);
                        // 数据
                        model.addAttribute("artists",artistIPage.getRecords());
                        // 擅长艺术种类
                        model.addAttribute("type",type);
                        // 艺术家名字模糊搜索
                        model.addAttribute("name",name);
                        // 当前页
                        model.addAttribute("currentPage",artistIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages",artistIPage.getPages());
                        // 分页页码
                        model.addAttribute("pages",getPageList((int) artistIPage.getPages(),currentPage,5));

                        model.addAttribute("artTypes",artTypes);
                    }
                    // 艺术品 3
                    if(departContentSectionServiceList.get(j).getContentType().equals(3)){
                        // 显示艺术类型
                        artTypes = artTypeService.getList();
                        model.addAttribute("artTypes",artTypes);


                        Artwork searchItem = new Artwork();
                        searchItem.setName(name);
                        searchItem.setContentId(departContentSectionServiceList.get(j).getContentId());
                        if(type!=null){
                            for (int k = 0; k < artTypes.size(); k++) {
                                if (Objects.equals(artTypes.get(k).getId(), type)){
                                    searchItem.setArtwoktType(artTypes.get(k).getTypeId());
                                }
                            }
                        }
                        List<Artwork> artworkTypeList = new ArrayList<>();
                        IPage<Artwork> artworkIPage = new Page<>(currentPage,10);
                        // 作品在线
                        if(tempPath.equals("/zpzx")||tempPath.equals("pc/depart/depart_artwork_online")){
                            tempPath = "pc/depart/depart_artwork_online";
                            // 作品在线
                            searchItem.setType(1);
                            // 机构uuid
                            searchItem.setDepartId(deaprtId);
                            Integer size = 10;
                            artworkIPage = artwrokService.getDepartRelatedArtworkPageList(searchItem,currentPage,size);
                            artworkTypeList = artwrokService.getDepartRelatedArtworkTypegetList(searchItem);

                        }else{
                            Integer size = 10;
                            artworkTypeList = artwrokService.getRelatedArtworkArtTypegetList(searchItem);
                            artworkIPage = artwrokService.getRelatedArtworkPageList(searchItem,currentPage,size);
                            searchItem.setType(0);
                        }
                        for (int k = 0; k < artworkTypeList.size(); k++) {
                            for (int z = 0; z < artTypes.size(); z++) {
                                if(artworkTypeList.get(k).getArtwoktType().equals(artTypes.get(z).getTypeId())){
                                    artTypes.get(z).setStatus(3);
                                    System.out.println(artTypes.get(z).getName());
                                }
                            }
                        }

                        // 名字模糊搜索
                        model.addAttribute("name",name);
                        // 数据
                        model.addAttribute("artworks",artworkIPage.getRecords());
                        // 当前页
                        model.addAttribute("currentPage",artworkIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages",artworkIPage.getPages());
                        // 分页页码
                        model.addAttribute("pages",getPageList((int) artworkIPage.getPages(),currentPage,3));

                    }
                    // 文章 5
                    if(departContentSectionServiceList.get(j).getContentType().equals(5)){
                        IPage<Article> articleIPage = articleService.getDepartRelatedArticlePageList(departContentSectionServiceList.get(j),currentPage);

                        //articles = articleService.getDepartRelateArticleList(departContentSectionServiceList.get(j),2);
                        // 数据
                        model.addAttribute("articles",articleIPage.getRecords());
                        // 当前页
                        model.addAttribute("currentPage",articleIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages",articleIPage.getPages());
                        // 分页页码
                        model.addAttribute("pages",getPageList((int) articleIPage.getPages(),currentPage,3));
                    }
                    // 超连接 6
                    if(departContentSectionServiceList.get(j).getContentType().equals(6)){

                        if(departContentSectionServiceList.get(j).getContent()!= null){
                            return "redirect:"+departContentSectionServiceList.get(j).getContent();
                        }
                    }
                    // 活动展览 7
                    if(departContentSectionServiceList.get(j).getContentType().equals(7)){
                        // 活动展览
                        EventExhibition eventExhibition = new EventExhibition();
                        eventExhibition.setContentId(departContentSectionServiceList.get(j).getContentId());

                        IPage<EventExhibition> eventExhibitionIPage = eventExhibitionService.getDepartRelatedEventExhibitionPageList(eventExhibition,currentPage);

                        // 数据
                        List<EventExhibition> records = eventExhibitionIPage.getRecords();
                        for (int l = 0; l < records.size(); l++) {
                            if(records.get(l).getEndTime().isBefore(LocalDateTime.now())){
                                records.get(l).setIsOver(1);

                            }
                            List<Artist> list = new ArrayList<>();
                            String[] strings1 = records.get(l).getArtistIds().split("，");
                            String[] strings2 = records.get(l).getArtistNames().split("，");
                            for (int z = 0; z < strings1.length; z++) {
                                if(!strings1[z].equals("")){
                                    Artist artistItem = new Artist();
                                    artistItem.setId(Integer.valueOf(strings1[z]));
                                    artistItem.setName(strings2[z]);
                                    list.add(artistItem);
                                }
                            }
                            records.get(l).setList(list);
                        }



                        model.addAttribute("eventExhibitions",eventExhibitionIPage.getRecords());
                        // 当前页
                        model.addAttribute("currentPage",eventExhibitionIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages",eventExhibitionIPage.getPages());
                        // 分页页码
                        model.addAttribute("pages",getPageList((int) eventExhibitionIPage.getPages(),currentPage,3));
                    }
                }
                DepartTemp departTempItem = new DepartTemp();
                departTempItem.setDepartId(deaprtId);
                DepartTemp departTemp = departTempService.getDepartTemp(departTempItem);
                if(departTemp!= null){
                    model.addAttribute("departTemp",departTemp);
                }else{
                    return "redirect:/";
                }



                // 模板单页数据
                model.addAttribute("pageDetails",pages);



            }
        }
        model.addAttribute("departSectionTemps",departSectionTemps);

        // departTemp/getDepartTemp
        System.out.println(tempPath+"模板");
        switch (tempPath){
            case "pc/depart/simple_page":
                tempPath = "mobile/depart/page";
                System.out.println("单页");
                break;
            case "pc/depart/simple_artist_list":
                tempPath = "mobile/depart/artist_list";
                System.out.println("艺术家列表");
                break;
            case "pc/depart/depart_artwork_online":
                tempPath = "mobile/depart/online_list";
                System.out.println("作品在线列表");
                break;
            case "pc/depart/simple_article_list":
                tempPath = "mobile/depart/article_list";
                System.out.println("文章列表");
                break;
            case "pc/depart/simple_artwork_list":
                tempPath = "mobile/depart/artwork_list";
                System.out.println("作品列表");
                break;
            case "pc/depart/depart_artwork_commission":
                tempPath = "mobile/depart/online_list";
                System.out.println("作品在线列表");
                break;
            case "pc/depart/simple_event_list":
                tempPath = "mobile/depart/event_list";
                System.out.println("活动列表");
                break;
        }


        if(tempPath.equals("")){
            return "redirect:/";
        }else{

            return tempPath;
        }
    }





}
